package com.tanhua.server.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.mapper.PublishInfoMapper;
import com.tanhua.common.pojo.PublishInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class QuanZiVerifyService {

    @Autowired
    private PublishInfoMapper publishInfoMapper;

    public void insertPublishInfo(PublishInfo publishInfo){
        int count = this.publishInfoMapper.insert(publishInfo);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    public List<PublishInfo> queryPublishInfo() {
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        List<PublishInfo> publishInfoList = this.publishInfoMapper.selectList(queryWrapper);
        return publishInfoList;
    }

    /**
     * 根据publishId查询数据
     * @param publishId
     * @return
     */
    public PublishInfo queryPublishInfoByPublishId(String publishId){
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("publish_id",publishId);
        PublishInfo publishInfo = this.publishInfoMapper.selectOne(queryWrapper);
        return publishInfo;
    }

    /**
     * 审核通过
     *
     * @return
     */
    public List<String> pass(List<String> publishIds) {
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("publish_id", publishIds);
        List<PublishInfo> publishInfoList = this.publishInfoMapper.selectList(queryWrapper);
        List<String> passList = new ArrayList<>();
        for (PublishInfo publishInfo : publishInfoList) {
            String state = publishInfo.getState();
            if (StrUtil.equals(state,"3")){
                passList.add(publishInfo.getPublishId());
            }
        }
        UpdateWrapper<PublishInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("publish_id", passList);
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setState("5");
        publishInfo.setUpdated(new Date());
        publishInfo.setCreated(new Date());
        this.publishInfoMapper.update(publishInfo, updateWrapper);
        return passList;
    }

    /**
     * 审核不通过
     *
     * @return
     */
    public List<String> reject(List<String> publishIds) {
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("publish_id", publishIds);
        List<PublishInfo> publishInfoList = this.publishInfoMapper.selectList(queryWrapper);
        List<String> rejectList = new ArrayList<>();
        for (PublishInfo publishInfo : publishInfoList) {
            String state = publishInfo.getState();
            if (StrUtil.equals(state,"3")){
                rejectList.add(publishInfo.getPublishId());
            }
        }
        UpdateWrapper<PublishInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("publish_id", rejectList);
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setState("4");
        publishInfo.setUpdated(new Date());
        publishInfo.setCreated(new Date());
        this.publishInfoMapper.update(publishInfo, updateWrapper);
        return rejectList;
    }

    /**
     * 撤销
     *
     * @return
     */
    public List<String> revoke(List<String> publishIds) {
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("publish_id", publishIds);
        List<PublishInfo> publishInfoList = this.publishInfoMapper.selectList(queryWrapper);
        List<String> revokeList = new ArrayList<>();
        List<String> revokePassList = new ArrayList<>();
        for (PublishInfo publishInfo : publishInfoList) {
            String state = publishInfo.getState();
            if (!StrUtil.equals(state,"3")){
                revokeList.add(publishInfo.getPublishId());
            }
            if (StrUtil.equals(state,"2") || StrUtil.equals(state,"5")){
                revokePassList.add(publishInfo.getPublishId());
            }
        }
        UpdateWrapper<PublishInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("publish_id", revokeList);
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setState("3");
        publishInfo.setUpdated(new Date());
        publishInfo.setCreated(new Date());
        this.publishInfoMapper.update(publishInfo, updateWrapper);
        return revokePassList;
    }

    public IPage<PublishInfo> queryPublishInfo(QueryWrapper queryWrapper,IPage<PublishInfo> iPage){
        IPage page = this.publishInfoMapper.selectPage(iPage, queryWrapper);
        return page;
    }

    public PublishInfo queryPublishInfoById(String id){
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        PublishInfo publishInfo = this.publishInfoMapper.selectOne(queryWrapper);
        return publishInfo;
    }

    public void top(String id) {
        UpdateWrapper<PublishInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setTopState(2);
        publishInfo.setUpdated(new Date());
        this.publishInfoMapper.update(publishInfo,updateWrapper);
    }

    public void untop(String id) {
        UpdateWrapper<PublishInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setTopState(1);
        publishInfo.setUpdated(new Date());
        this.publishInfoMapper.update(publishInfo,updateWrapper);
    }

    public List<PublishInfo> queryPublishInfoByIds(List<String> ids){
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids);
        List<PublishInfo> publishInfoList = this.publishInfoMapper.selectList(queryWrapper);
        return publishInfoList;
    }
}
